package com.liyunc.demo.bundle.file.manager.web.resource;

import com.liyunc.demo.bundle.file.manager.model.dto.input.AssetPackageRequestDto;
import com.liyunc.demo.bundle.file.manager.model.dto.output.AssetPackageDto;
import com.liyunc.demo.bundle.file.manager.service.AssetPackageService;
import com.liyunc.demo.comp.common.rest.response.GeneralResponse;
import com.liyunc.demo.comp.common.utils.HttpResponseUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.util.Objects;
import java.util.Optional;

/**
 * @author liyuncong
 * @version 1.0
 * @file AssetPackageResource
 * @brief AssetPackageResource
 * @details AssetPackageResource
 * @date 2022-04-15
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-04-15                      liyuncong          Created
 */
@RequestMapping(
    value = "/api/v1/packed-asset",
    produces = MediaType.APPLICATION_JSON_VALUE
)
@Log4j2
@Validated
public class AssetPackageResource {

    private static final String TAR_ZIP_CONTENT_TYPE = "application/gzip";

    @Autowired
    private AssetPackageService packageService;

    @RequestMapping(
        method = RequestMethod.POST,
        path = "",
        consumes = {MediaType.APPLICATION_JSON_VALUE}
    )
    public GeneralResponse<String> packAndCompress(
        @Valid @NotNull @RequestBody AssetPackageRequestDto requestDto) {
        return GeneralResponse.of(
            packageService.packAndCompress(requestDto).orElse(null)
        );
    }

    @RequestMapping(
        method = RequestMethod.GET,
        path = "/{tar_id}"
    )
    public ResponseEntity<InputStreamResource> download(
        @Valid @NotNull @Min(0) @PathVariable("tar_id") Long tarId
    ) {
        Optional<Pair<File, AssetPackageDto>> result = packageService.download(tarId);
        if (result.isEmpty()) {
            return ResponseEntity
                .status(HttpStatus.NOT_FOUND)
                .build();
        }
        final File file = result.get().getLeft();
        final AssetPackageDto packageDto = result.get().getRight();
        if (Objects.isNull(file) || Objects.isNull(packageDto)) {
            return ResponseEntity
                .status(HttpStatus.NOT_FOUND)
                .build();
        }
        if (packageDto.getPackStatus() == 0) {
            return ResponseEntity
                .status(HttpStatus.NO_CONTENT)
                .build();
        }
        final String filename = file.getName();
        return HttpResponseUtils.sendAttachment(filename, TAR_ZIP_CONTENT_TYPE, file);
    }
}
